package com.xxxy.jsq.action.works;

import com.xxxy.jsq.dao.WorksDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/deleteWorksServlet")
public class deleteWorksServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteWorksServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码，防止中文乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        // 实例化DAO（模型对象可省略，仅需ID即可删除）
        WorksDao dao = new WorksDao();

        // 获取前端传递的作品ID（与前端删除请求参数保持一致）
        Long worksId = Long.valueOf(request.getParameter("cid"));

        // 调用DAO的deleteById方法执行删除
        String result = dao.deleteById(worksId);

        // 输出删除结果给前端
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // POST请求复用GET逻辑，保持与部门删除Servlet一致
        doGet(request, response);
    }
}
